package com.itboy.mall.pms.category.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.itboy.common.annotation.RepeatSubmit;
import com.itboy.common.annotation.Log;
import com.itboy.common.core.controller.BaseController;
import com.itboy.common.core.domain.AjaxResult;
import com.itboy.common.enums.BusinessType;
import com.itboy.mall.pms.category.vo.MallCategoryVo;
import com.itboy.mall.pms.category.bo.MallCategoryQueryBo;
import com.itboy.mall.pms.category.bo.MallCategoryAddBo;
import com.itboy.mall.pms.category.bo.MallCategoryEditBo;
import com.itboy.mall.pms.category.service.IMallCategoryService;
import com.itboy.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商品分类Controller
 *
 * @author itboy
 * @date 2021-07-13
 */
@Api(value = "商品分类控制器", tags = {"商品分类管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mall/category")
public class MallCategoryController extends BaseController {

    private final IMallCategoryService iMallCategoryService;

    /**
     * 查询商品分类列表
     */
    @ApiOperation("查询商品分类列表")
    @PreAuthorize("@ss.hasPermi('mall:category:list')")
    @GetMapping("/list")
    public AjaxResult<List<MallCategoryVo>> list(@Validated MallCategoryQueryBo bo) {
        List<MallCategoryVo> list = iMallCategoryService.queryList(bo);
        return AjaxResult.success(list);
    }

    /**
     * 导出商品分类列表
     */
    @ApiOperation("导出商品分类列表")
    @PreAuthorize("@ss.hasPermi('mall:category:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MallCategoryVo> export(@Validated MallCategoryQueryBo bo) {
        List<MallCategoryVo> list = iMallCategoryService.queryList(bo);
        ExcelUtil<MallCategoryVo> util = new ExcelUtil<MallCategoryVo>(MallCategoryVo.class);
        return util.exportExcel(list, "商品分类");
    }

    /**
     * 获取商品分类详细信息
     */
    @ApiOperation("获取商品分类详细信息")
    @PreAuthorize("@ss.hasPermi('mall:category:query')")
    @GetMapping("/{id}")
    public AjaxResult<MallCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") String id) {
        return AjaxResult.success(iMallCategoryService.queryById(id));
    }

    /**
     * 新增商品分类
     */
    @ApiOperation("新增商品分类")
    @PreAuthorize("@ss.hasPermi('mall:category:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MallCategoryAddBo bo) {
        return toAjax(iMallCategoryService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改商品分类
     */
    @ApiOperation("修改商品分类")
    @PreAuthorize("@ss.hasPermi('mall:category:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MallCategoryEditBo bo) {
        return toAjax(iMallCategoryService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除商品分类
     */
    @ApiOperation("删除商品分类")
    @PreAuthorize("@ss.hasPermi('mall:category:remove')")
    @Log(title = "商品分类" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable String[] ids) {
        return toAjax(iMallCategoryService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
